package com.hui.mapper;

import com.hui.dto.DeptPageDTO;
import com.hui.dto.EmpPageDTO;
import com.hui.entity.Dept;
import com.hui.entity.Emp;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EmpMapper {

    @Insert("""
        insert into ums_emp (
        username, password, avatar, phone, wechat, email, gender, age, province, 
        address, realname, idcard, info, fk_dept_id, hiredate, deleted, created, updated)
        values (
        #{username}, #{password}, #{avatar}, #{phone}, #{wechat}, #{email}, #{gender}, #{age}, #{province}, 
        #{address}, #{realname}, #{idcard}, #{info}, #{fkDeptId}, #{hiredate}, #{deleted}, #{created}, #{updated})
    """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Emp emp);

    @Results(id = "empResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "fkDeptId", column = "fk_dept_id"),
            @Result(property = "dept", column = "fk_dept_id",
            one = @One(select = "com.hui.mapper.DeptMapper.select")),
    })
    @Select("""
            select * from ums_emp t
            where t.id = #{param1} and t.deleted = 0
            """)
    Emp select(Long id);

    @ResultMap("empResultMap")
    @Select("""
            <script>
            select * from ums_emp t
            <where>
            <if test='username != null'> username = #{username} and </if>
            <if test='realname != null'> realname = #{realname} and </if>
            <if test='phone != null'> phone = #{phone} and </if>
            <if test='idcard != null'> idcard = #{idcard} and </if>
            <if test='fkDeptId != null'> fk_dept_id = #{fkDeptId} and </if>
            t.deleted = 0
            </where>
            </script>
            """)
    List<Emp> list(EmpPageDTO dto);

    @Update("""
            <script>
            update ums_emp
            <set>
            <if test='username != null'> username = #{username},</if>
            <if test='password != null'> password = #{password},</if>
            <if test='avatar != null'> avatar = #{avatar},</if>
            <if test='phone != null'> phone = #{phone},</if>
            <if test='wechat != null'> wechat = #{wechat},</if>
            <if test='email != null'> email = #{email},</if>
            <if test='gender != null'> gender = #{gender},</if>
            <if test='age != null'> age = #{age},</if>
            <if test='province != null'> province = #{province},</if>
            <if test='address != null'> address = #{address},</if>
            <if test='realname != null'> realname = #{realname},</if>
            <if test='idcard != null'> idcard = #{idcard},</if>
            <if test='fkDeptId != null'> fk_dept_id = #{fkDeptId},</if>
            <if test='info != null'> info = #{info},</if>
            <if test='updated != null'> updated = #{updated},</if>
            <if test='deleted != null'> deleted = #{deleted},</if>
            <if test='version != null'> version = #{version},</if>
            version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version} 
            </script>
            """)
    int update(Emp emp);

    @Update("""
            update ums_emp
            set deleted = 1, updated = current_timestamp
            where id = #{id} 
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_emp
            set deleted = 1, updated = current_timestamp
            where id in
            <foreach collection='list'  item='e'  open='id in(' close=')' separator=','>
            #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);



    @Update("""
            update ums_emp
            set password = #{param1}, updated = now()
            where password = #{param2} and id = #{param3} and deleted = 0
            """)
    int updatePassword(String newPassword, String oldPassword, Long id);

    @ResultMap("empResultMap")
    @Select("""
            select * from ums_emp t 
            where t.username = #{param1} and t.password = #{param2} and t.deleted = 0
            """)
    Emp selectByAccount(String username, String password);



}
